package com.arron.juc;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * @Classname CallableDemo
 * @Description TODO
 * @Date 2020/8/21 2:55
 * @Created by 刘
 */

class Task implements Callable<Integer>{

    @Override
    public Integer call() throws Exception {
        System.out.println(Thread.currentThread().getName()+"\t处理逻辑中。。。");
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return 200;
    }
}

public class CallableDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Task task = new Task();
        FutureTask<Integer> integerFutureTask = new FutureTask<>(task);
        //开启一个线程执行耗时较长的任务
        new Thread(integerFutureTask,"A").start();

        /**
         * 处理自己的逻辑
         */
        System.out.println(Thread.currentThread().getName()+"\t处理自己的逻辑中。。。。。。。。");
        try {
            TimeUnit.SECONDS.sleep(6);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"\t 处理完毕 拿到任务处理结果"+integerFutureTask.get());
    }

}
